/**
 * @Author: Nico
 */
// import isEmpty from 'lodash/isEmpty'
// if (
//   model_info &&
//   model_info.extension.defaultValue &&
//   !isEmpty(model_info.extension.defaultValue.value) &&
//   model_info.extension.ignoreClearValue &&
//   model_info.extension.ignoreClearValue.value
// ) {
//   let _v = model_info.extension.defaultValue.value
//   defaultValue[model_info.extension.model.value] = model_info.type == 'model_info' ? Number(_v) : _v
// }
// let ref = _form.ref
//先清空原有表单
// if (ref) {
//   ref.resetFields()
// }

import { getComponentDefaulType, processingConstant, getDefaultDate } from '@ve/utils/utils'
import { getModelInfo } from '@ve/utils/getModelInfo'
import isArray from 'lodash/isArray'
import keys from 'lodash/keys'
import includes from 'lodash/includes'
import cloneDeep from 'lodash/cloneDeep'
//必须带done回调 执行完成调用 vm, action, done
function resetFormData(formData, formItems, vm) {
  let defaultValue = {}
  if (formItems) {
    Object.keys(formItems).forEach((v) => {
      let model_info = getModelInfo(vm, v)
      // Nico处理下拉选

      if (v.indexOf('uiPdaSelect') != '-1') {
        // 清空表单里面的字段中文值
        console.log('清空表单里面的字段中文值1', v, model_info.extension.model.value, vm.vuiComponents[v].self)
        formData[`${model_info.extension.model.value}CODE`] = ''
        // 清空页面的中文
        vm.vuiComponents[v].self.formDataModelLabel = ''
        vm.vuiComponents[v].self.formDataModel = ''
        console.log('清空表单里面的字段中文值2', v, model_info.extension.model.value, vm.vuiComponents[v].self)
      }
      console.log('测试清空表单', formItems)
      if (v.indexOf('uiPdaUpload') != '-1') {
        console.log('1=1=1=1', vm.vuiComponents[v].self)
        vm.vuiComponents[v].self.fileList = []
      }

      if (isArray(model_info)) return
      if (model_info && model_info.extension.ignoreClearValue && model_info.extension.ignoreClearValue.value) {
        defaultValue[model_info.extension.model.value] = formData[model_info.extension.model.value]
      } else {
        // 赋默认值
        if (model_info && model_info.extension.defaultValue) {
          // console.log(`触发寻找默认值~~~~~~~~${model_info._id} --------------------- ${model_info.extension.defaultValue.value}`)
          defaultValue[model_info.extension.model.value] = processingConstant(vm, model_info.extension.defaultValue.value)
          //选择新
          if (model_info.type.indexOf('uiSelectLayerNew') !== -1) {
            defaultValue[model_info.extension.fieldKey.value] = processingConstant(vm, model_info.extension.defaultValue.value)
          }
        } else {
          //选择新
          if (model_info.type.indexOf('uiSelectLayerNew') !== -1) {
            defaultValue[model_info.extension.fieldKey.value] = ''
          }
          //开关按钮单独获取值
          if (model_info.type === 'uiSwitch') {
            let switchValue = null
            if (model_info.extension.defaultState.value) {
              switchValue = model_info.extension.activeValue.value
            } else {
              switchValue = model_info.extension.inactiveValue.value
            }
            defaultValue[model_info.extension.model.value] = switchValue
          } else if (model_info.type === 'uiDate') {
            // 日期时间组件单独获取值
            if (model_info.extension.defaultDate.value) {
              // 如果设置默认值为 当前
              if (model_info.extension.selection.value === 'datetime' || model_info.extension.selection.value === 'date') {
                // 如果当前组件类型为 datetime 或 date
                if (model_info.extension.defaultDate.value === 'now') {
                  // 设置 默认值为 当前 的默认值
                  defaultValue[model_info.extension.model.value] = getDefaultDate('now')
                  // 如果为日期类型则去掉值的时分秒
                  if (model_info.extension.selection.value.indexOf('time') == -1) {
                    defaultValue[model_info.extension.model.value] = defaultValue[model_info.extension.model.value].split(' ')[0]
                  }
                } else {
                  defaultValue[model_info.extension.model.value] = ''
                }
              } else if (model_info.extension.selection.value === 'datetimerange' || model_info.extension.selection.value === 'daterange') {
                // 如果当前组件类型为 时间范围
                const startSuffix = model_info.extension?.startSuffix?.value ? model_info.extension?.startSuffix?.value : '_start'
                const endSuffix = model_info.extension?.endSuffix?.value ? model_info.extension?.endSuffix?.value : '_end'
                if (model_info.extension.defaultDate.value !== 'now') {
                  defaultValue[model_info.extension.model.value] = getDefaultDate(model_info.extension.defaultDate.value)

                  defaultValue[`${model_info.extension.model.value}${startSuffix}`] = defaultValue[model_info.extension.model.value][0] || ''
                  defaultValue[`${model_info.extension.model.value}${endSuffix}`] = defaultValue[model_info.extension.model.value][1] || ''
                  // 如果为日期类型则去掉值的时分秒
                  if (model_info.extension.selection.value.indexOf('time') == -1) {
                    defaultValue[model_info.extension.model.value][0] = defaultValue[model_info.extension.model.value][0].split(' ')[0]
                    defaultValue[model_info.extension.model.value][1] = defaultValue[model_info.extension.model.value][1].split(' ')[0]
                    defaultValue[`${model_info.extension.model.value}${startSuffix}`] =
                      defaultValue[`${model_info.extension.model.value}${startSuffix}`].split(' ')[0]
                    defaultValue[`${model_info.extension.model.value}${endSuffix}`] =
                      defaultValue[`${model_info.extension.model.value}${endSuffix}`].split(' ')[0]
                  }
                } else {
                  defaultValue[model_info.extension.model.value] = ''
                  defaultValue[`${model_info.extension.model.value}${startSuffix}`] = ''
                  defaultValue[`${model_info.extension.model.value}${endSuffix}`] = ''
                }
              }
            } else {
              // 没有默认值则置为空
              defaultValue[model_info.extension.model.value] = ''
              if (model_info.extension.selection.value === 'datetimerange' || model_info.extension.selection.value === 'daterange') {
                // 如果是时间范围类型 则需要将外层的值也清空
                const startSuffix = model_info.extension?.startSuffix?.value ? model_info.extension?.startSuffix?.value : '_start'
                const endSuffix = model_info.extension?.endSuffix?.value ? model_info.extension?.endSuffix?.value : '_end'
                defaultValue[`${model_info.extension.model.value}${startSuffix}`] = ''
                defaultValue[`${model_info.extension.model.value}${endSuffix}`] = ''
              }
            }
          } else if (model_info.type === 'uiSelectPrinters') {
            defaultValue[`${model_info.extension.model.value}`] = ''
            defaultValue[`${model_info.extension.modelPaper.value}`] = ''
          } else {
            defaultValue[model_info.extension.model.value] = getComponentDefaulType(model_info.type)
          }
        }
      }
    })
  }
  return defaultValue
}
export default function pdaFormClear({ vm, action, done }) {
  console.log('来了没')
  if (action.extension.formSelect.value.length > 0) {
    action.extension.formSelect.value.forEach((item) => {
      let _form = vm.vuiComponents[item]

      if (_form) {
        let self = _form.self
        let getValues = resetFormData(self.getInfo.data.formData, self.getInfo.data.formItems, vm)

        // 先查找改变了的值
        const keysList = keys(getValues)
        let changeModelList = []
        keysList.forEach((keyItem) => {
          if (getValues[keyItem] !== self.getInfo.data.formData[keyItem]) {
            changeModelList.push(keyItem)
          }
        })
        //合并默认值
        self.getInfo.data.formData = {
          ...self.getInfo.data.formData,
          ...getValues,
        }
        // 清空_formData中的

        self.getInfo.data._formData = cloneDeep(self.getInfo.data.formData)
        // 提交 dispatch 触发页面组件状态更新（ dataObj 为传递的数据对象，moduleObj 为目标组件对象
        // vm.$store.dispatch('pageModulesState/setPageModulesStatesByObj', {
        //   pageId: vm.$store.getters['preview/getNowPageId'],
        //   pageState: { dataObj: self.getInfo.data.formData, moduleObj: self.getInfo },
        // })
        // 触发改变了的组件的值改变事件
        const eachFunc = (obj) => {
          const modelVal = obj.extension?.model?.value
          // 查找组件的 model 值绑定在修改值对象中
          if (modelVal && includes(changeModelList, modelVal)) {
            // 触发组件的值改变事件
            vm.vuiComponents[obj._id]?.self?.onChange()
          }
          obj.pageModules &&
            obj.pageModules.forEach((item) => {
              eachFunc(item)
            })
          obj.list &&
            obj.list.forEach((item) => {
              eachFunc(item)
            })
        }
        eachFunc(self.getInfo)
      } else {
        console.log('没找到form组件')
      }
    })
  }
  done(null, {})
}
